package com.jt.assistant.custom_view

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.jt.assistant.R
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity

class TopBar : FrameLayout {

    constructor(context: Context) : super(context) {
        initView(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context, attrs)
    }

    lateinit var mIvBack: ImageButton
    lateinit var mTvRightTitle: TextView

    @SuppressLint("RtlHardcoded")
    private fun initView(context: Context, attrs: AttributeSet?) {
        val view: View = LayoutInflater.from(context).inflate(R.layout.item_top_bar, this, true)
        mIvBack = view.findViewById<ImageButton>(R.id.iv_back)
        val mTvTitle = view.findViewById<TextView>(R.id.tv_title)
        mTvRightTitle = view.findViewById<TextView>(R.id.tv_rightTitle)
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar)
        val title = typedArray.getString(R.styleable.TopBar_title)
        val isBack = typedArray.getBoolean(R.styleable.TopBar_isBack, true)
        val rightTitle = typedArray.getString(R.styleable.TopBar_rightTitle)


        if (title != null) {
            mTvTitle.text = title
        }

        if (isBack) {
            mIvBack.visibility = VISIBLE
        } else {
            mIvBack.visibility = GONE
        }

        if (!rightTitle.isNullOrEmpty()) {
            mTvRightTitle.text = rightTitle
        }
        mIvBack.setOnClickListener { (context as AppCompatActivity).finish() }
        typedArray.recycle()
    }
}


